<?php
    error_reporting(E_ALL & ~E_DEPRECATED); 
    require_once("condb.php");     
    require_once("functions/functions_global.php");     
    require_once('libs/nusoap.php');
    
    $server = new soap_server();
    
    $ns = "urn:PaymentServices";
    $server->configureWSDL("PaymentServices", $ns);
    $server->wsdl->schemaTargetNamespace = $ns;    
    
    $server->wsdl->addComplexType(
        'response'
        , 'complexType'
        , 'struct'
        , 'all'
        , ''
        , array(
            'status'=>array(
                'name'=>'status'
                , 'type'=>'xsd:int'
            ),
            'msg'=>array(
                'name'=>'msg'
                , 'type'=>'xsd:string'
            )
        )
    );           
    
    $server->register(
        "call"
        , array("content"=>"xsd:string", "sender"=>"xsd:string", "serviceCode"=>"xsd:string", "transactionId"=>"xsd:string")
        , array("return"=> "tns:response")
//        , array("return"=> "xsd:string")
        , $ns
        , $ns . "#call"
        , "rpc"            
        , "encoded"
        , "Examination Results"  
    ); 
    
    function call($content, $sender, $serviceCode, $transactionId)
    {       
        $ip = getRealIpAddr();
        $IpPublic = IpPublic();
        $response = array();
        if(in_array($ip, $IpPublic)){
            
            $data = array();
            $data["content"] = preg_replace("/(\s+)/si", " ", $content);        
            $data["sender"] = $sender;
            $data["serviceCode"] = $serviceCode;
            $data["transactionId"] = $transactionId;    
            
            /* Kiểm tra transaction */
            $checkTransaction = CheckTransaction($transactionId);
            
            if($checkTransaction==0){
                $cp = false;
                
                /*
                * check các cú pháp của app
                */
                
                $syntax = getSmsSyntax();
                $list_syntax = "";
                foreach($syntax as $row){
                    $list_syntax .= "|" . $row["syntax"];
                }
                $list_syntax = ltrim($list_syntax, "|");
                                
                if(preg_match("/^TVTM/si", $data["content"])){
                    
                    /*
                    * TeaMobi
                    */
                    $cp = true;
                    require_once("functions/functions_teamobi.php");                
                    
                } else if(preg_match("/^VAKT/si", $data["content"]) || preg_match("/^TUHA/si", $data["content"]) || preg_match("/^VALT/si", $data["content"])){                
                    
                    /*
                    * MECORP
                    */
                    $cp = true;
                    require_once("functions/functions_mecorp.php");                
                    
                } else if(preg_match("/^YO/si", $data["content"]) || preg_match("/^TVCH/si", $data["content"]) || preg_match("/^STAB/si", $data["content"])){                
                    
                    /*
                    * Sunnet
                    */
                    $cp = true;
                    require_once("functions/functions_sunnet.php");                
                    
                } else if(preg_match("/^TVFM/si", $data["content"]) || preg_match("/^TVBC/si", $data["content"]) ){                
                                   
                    /*
                    * FMC
                    */
                    
                    $cp = true;
                    require_once("functions/functions_fmc.php");                
                    
                } else if(preg_match("/^TVBG/si", $data["content"])){                
                    
                    /*
                    * Beme
                    */
                    $cp = true;
                    require_once("functions/functions_bgate.php");                
                    
                } else if(preg_match("/^TVB/si", $data["content"])){                
                    
                    /*
                    * Beme
                    */
                    $cp = true;
                    require_once("functions/functions_beme.php");                
                    
                } else if(preg_match("/^SMS REG1/si", $data["content"]) || preg_match("/^SMS REG2/si", $data["content"]) || preg_match("/^SMS REG3/si", $data["content"]) || preg_match("/^SMS REG4/si", $data["content"])){                
                    
                    /*
                    * Tin nhan kute
                    */
                    
                    $cp = true;
                    require_once("functions/functions_sms_cute_active.php");                
                    
                }  else if(preg_match("/^SMS/si", $data["content"])){                
                    
                    /*
                    * Tin nhan kute
                    */
                    
                    $cp = true;
                    require_once("functions/functions_sms_cute.php");                
                    
                } else if(preg_match("/^TEST/si", $data["content"])){                
                    
                    /*
                    * Tin nhan kute
                    */
                    
                    $cp = true;
                    require_once("functions/functions_test.php");                
                    
                } else if(preg_match("/^TVA GAME TVZO/si", $data["content"])){                
                    
                    /*
                    * Tin nhan kute
                    */
                    
                    $cp = true;
                    require_once("functions/functions_zozo.php");                
                    
                } else if(preg_match("/^TVA MOD/si", $data["content"])){                
                    
                    /*
                    * Tin nhan kute
                    */
                    
                    $cp = true;
                    require_once("functions/functions_mod.php");                
                    
                } else if(preg_match("/^TVA IBET/si", $data["content"])){                
                    
                    /*
                    * Tin nhan kute
                    */
                    
                    $cp = true;
                    require_once("functions/functions_ibet.php");                
                    
                } else if(preg_match("/^TVA OLWP/si", $data["content"])){                
                    
                    /*
                    * vien thong
                    */
                    
                    $cp = true;
                    require_once("functions/functions_vienthong.php");                
                    
                } else if(preg_match("/^TVA GAMEIWIN/si", $data["content"]) || preg_match("/^TVA GAMEGOP/si", $data["content"]) || preg_match("/^TVA GAMEGOF/si", $data["content"]) || preg_match("/^TVA GAMETLTH/si", $data["content"])){
                    
                    /*
                    * Tin nhan kute
                    */
                    
                    $cp = true;
                    require_once("functions/functions_mecorp_new.php");                
                    
                } else if(!empty($list_syntax) && preg_match("/^(" . $list_syntax . ")/si", $data["content"])) {
                    /*
                    * Lấy type xem là loại app gì
                    */
                    preg_match("/^(" . $list_syntax . ")/si", $data["content"], $match);    
                    $syntax_code_db = !empty($match[1]) ? $match[1] : "";
                    $st = getTypeSyntax($syntax_code_db);
                    
                    if(!empty($st)){                        
                        $data["syntax_code_db"] = $syntax_code_db;
                        
                        if($st["type"]=="1" || $st["type"]=="2" || $st["type"]=="3" || $st["type"]=="7"){
                            
                            preg_match("/^(" . $list_syntax . ")(\s+)(\d+)/si", $data["content"], $match);
                            $type = $match[3];
                            $sc = getServiceCode($syntax_code_db, $type);                            
                            
                            /*
                            * HDC App
                            */  
                            $cp = true; 
                            $data["service_code_db"] = $sc["serviceCode"];                           
                            require_once("functions/functions_app.php");   
                        } else if($st["type"]=="6"){
                            /*
                            * HDC App
                            */
                            $cp = true;
                            $data["service_code_db"] = $st["serviceCode"];
                            require_once("functions/functions_ring_app.php");   
                        } else if($st["type"]=="8" || $st["type"]=="9" || $st["type"]=="10" || $st["type"]=="11"){
                            /*
                            * HDC App
                            */
                            $cp = true;
                            $data["service_code_db"] = $st["serviceCode"];
                            require_once("functions/functions_msoi.php");   
                        }       
                    }
                }
                
                if($cp == true){                    
                    $response = processing($data);    
                } else {
                    $response["status"] = 6;            
                    $response["msg"] = "Invalid Request";    
                }
                
                
            } else {
                $response["status"] = 4;            
                $response["msg"] = "Invalid Request";    
            }        
        } else {
            $response["status"] = 5;            
            $response["msg"] = "Invalid Request";                
        }
        
        if($response["status"]==""){
            $response["status"] = 0;
        }       
            
        return $response;
    }            
    
    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ""; 
    $server->service($HTTP_RAW_POST_DATA);
    exit();
?>
